package org.enhance.common.helper;//package org.enhance.core.helper;
//
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.springframework.core.env.ConfigurableEnvironment;
//import org.springframework.core.env.EnumerablePropertySource;
//import org.springframework.core.env.MutablePropertySources;
//import org.springframework.lang.NonNull;
//
//import java.util.Map;
//import java.util.Set;
//
///**
// * springboot1.x属性绑定器
// *
// * @author wenpanfeng 2022/11/23 10:57
// */
//@Slf4j
//public class PropertiesBinder {
//
//    private PropertiesBinder() {
//    }
//
//    /**
//     * 绑定属性到target
//     *
//     * @param properties properties
//     * @param prefix     绑定前缀
//     * @param target     目标对象
//     * @author wenpanfeng 2022/11/24 17:05
//     */
//    public static <T> void bindProperties(Map<Object, Object> properties, String prefix, T target) {
//        try {
//            log.info("------------>>>>>>>>>> start bind properties, prefix is {}, target is {}", prefix, target);
//            MapPropertySource mapPropertySource = new MapPropertySource("PropertiesBinder.bindProperties", properties);
//            MutablePropertySources propertySources = new MutablePropertySources();
//            propertySources.addLast(mapPropertySource);
//            PropertySourcesBinder binder = new PropertySourcesBinder(propertySources);
//            binder.bindTo(prefix, target);
//            log.info("------------>>>>>>>>>> end bind properties, prefix is {}, target is {}", prefix, target);
//        } catch (Exception ex) {
//            log.info("------------>>>>>>>>>> error bind properties, prefix is {}, target is {}", prefix, target);
//            throw new RuntimeException(String.format("Bind properties failed, prefix is [%s], target is [%s]", prefix, target));
//        }
//    }
//
//    /**
//     * 属性绑定
//     *
//     * @param configurableEnvironment Environment
//     * @param prefix                  属性前缀
//     * @param clazz                   target clazz
//     * @return T
//     * @author wenpanfeng 2022/11/23 11:04
//     */
//    public static <T> T bindProperties(ConfigurableEnvironment configurableEnvironment, String prefix, Class<T> clazz) {
//        try {
//            log.info("------------>>>>>>>>>> start bind properties, prefix is {}, clazz is {}", prefix, clazz);
//            PropertySourcesBinder propertySourcesBinder = new PropertySourcesBinder(configurableEnvironment);
//            T instance = clazz.newInstance();
//            propertySourcesBinder.bindTo(prefix, instance);
//            log.info("------------>>>>>>>>>> end bind properties, prefix is {}, clazz is {}", prefix, clazz);
//            return instance;
//        } catch (Exception ex) {
//            log.info("------------>>>>>>>>>> error bind properties, prefix is {}, clazz is {}", prefix, clazz);
//            throw new RuntimeException(String.format("Bind properties failed, prefix is [%s], Class is [%s]", prefix, clazz));
//        }
//    }
//
//    /**
//     * {@link org.springframework.core.env.PropertySource} that reads keys and values from a {@code Map} object.
//     *
//     * @author wenpanfeng
//     * @see org.springframework.core.env.PropertiesPropertySource
//     */
//    static class MapPropertySource extends EnumerablePropertySource<Map<Object, Object>> {
//
//        public MapPropertySource(String name, Map<Object, Object> source) {
//            super(name, source);
//        }
//
//        @Override
//        public Object getProperty(@NonNull String name) {
//            return source.get(name);
//        }
//
//        @Override
//        public boolean containsProperty(String name) {
//            return source.containsKey(name);
//        }
//
//        @NonNull
//        @Override
//        public String[] getPropertyNames() {
//            Set<Object> set = source.keySet();
//            if (CollectionUtils.isNotEmpty(set)) {
//                String[] result = new String[set.size()];
//                Object[] objects = set.toArray();
//                for (int i = 0; i < objects.length; i++) {
//                    result[i] = objects[i].toString();
//                }
//                return result;
//            }
//            return new String[0];
//        }
//
//    }
//}